package AdminController

import (
	"el/app/libs"
	"el/app/models/request"
	"el/app/service"

	"github.com/labstack/echo/v4"
)

type UserController struct {
}

func (my *UserController) Login(c echo.Context) error {
	login := new(request.Login)
	if err := c.Bind(login); err != nil {
		return libs.Error(c, 1, err.Error())
	}
	admin, err := service.Login(login)
	if err != nil {
		return libs.Error(c, 1, err.Error())
	}
	return libs.Ok(c, admin)
}

func (my *UserController) Out(c echo.Context) error {
	token := c.Request().Header.Get("Authorization")
	err := service.Out(token)
	if err != nil {
		return libs.Error(c, 1, err.Error())
	}
	return libs.Ok(c, nil)
}

func (my *UserController) Log(c echo.Context) error {
	paginate := libs.Paginate(c)
	logSearch := new(request.LogSearch)
	if err := c.Bind(logSearch); err != nil {
		return libs.Error(c, 1, err.Error())
	}

	log, err := service.Log(paginate, logSearch)
	if err != nil {
		return libs.Error(c, 1, err.Error())
	}
	return libs.Ok(c, log)
}
